local RoomScene = require 'packages.hearthstone.battlegrounds.handler.roomscene'
local control = (require 'packages.hearthstone.battlegrounds.handler.ui_emu')
--local PointItem = control.Point
--local Bg = require "packages.hearthstone.battlegrounds"

---@class Battlegrounds.ReqRecruitPhase: RequestHandler
---@field public room Battlegrounds.RoomBase
local ReqRecruitPhase = RequestHandler:subclass("Battlegrounds.ReqRecruitPhase")

function ReqRecruitPhase:initialize(player, data)
    RequestHandler.initialize(self, player)
    self.scene = RoomScene:new(self)
end


function ReqRecruitPhase:setup()
  self:updateCards()
end

function ReqRecruitPhase:finish()
  self:clearCards()
end

function ReqRecruitPhase:updateCards()
  local scene = self.scene
  local player = self.player
  local room = self.room
  
  -- 获取玩家当前的商店卡牌
  local shop_cards = player.shop or {}
  
  -- 保留被锁住的卡牌
  local locked_cards = {}
  -- 检查卡牌
  for _, card in ipairs(shop_cards) do
    if card.frozen then
      table.insert(locked_cards, card)
    end
  end
  -- 计算需要刷新的卡牌数量
  local tavern_level = player.tavern_level or 1
  local max_minions = math.min(tavern_level + 1, 7) -- 1级2个，2级3个...6级7个
  
  local needed_minions = max_minions - #locked_cards
  
  -- 生成新的随从卡牌
  local new_minions = {}
  if needed_minions > 0 then
    -- 这里应该调用引擎生成随机随从
    -- new_minions = room.engine:getRandomMinions(tavern_level, needed_minions)
    -- 临时实现：创建占位符
    for i = 1, needed_minions do
      table.insert(new_minions, {
        name = "Minion_" .. tavern_level .. "_" .. i,
        cost = tavern_level,
        lock = false,
        tavern_level = tavern_level
      })
    end
  end
  

  
  -- 更新玩家的商店
  player.shop = {}
  player.shop_spells = {}
  
  -- 添加锁定的卡牌
  for _, card in ipairs(locked_cards) do
    table.insert(player.shop, card)
  end
  
  for _, spell in ipairs(locked_spells) do
    table.insert(player.shop_spells, spell)
  end
  
  -- 添加新生成的卡牌
  for _, card in ipairs(new_minions) do
    table.insert(player.shop, card)
  end
  
  for _, spell in ipairs(new_spells) do
    table.insert(player.shop_spells, spell)
  end
  
  -- 发送商店更新日志
  room:sendLog {
    type = "#BattlegroundsShopUpdated",
    arg = player.id,
    arg2 = #player.shop,
    arg3 = #player.shop_spells,
  }
end

function ReqRecruitPhase:clearCards()
  local scene = self.scene
  for _, id in ipairs(self.cards) do
    scene:removeItem("Battlegrounds.CardItem", id)
  end
  self.cards = {}
end

--- 锁定/解锁商店中的卡牌
---@param card_index integer @ 卡牌索引
---@param card_type string @ 卡牌类型："minion" 或 "spell"
---@param lock boolean @ 是否锁定
function ReqRecruitPhase:toggleCardLock(card_index, card_type, lock)
  local player = self.player
  local room = self.room
  
  if card_type == "minion" then
    if card_index >= 1 and card_index <= #player.shop then
      player.shop[card_index].lock = lock
      
      room:sendLog {
        type = "#BattlegroundsCardLocked",
        arg = player.id,
        arg2 = card_index,
        arg3 = lock,
      }
    end
  elseif card_type == "spell" then
    if card_index >= 1 and card_index <= #player.shop_spells then
      player.shop_spells[card_index].lock = lock
      
      room:sendLog {
        type = "#BattlegroundsSpellLocked",
        arg = player.id,
        arg2 = card_index,
        arg3 = lock,
      }
    end
  end
end

--- 购买商店中的卡牌
---@param card_index integer @ 卡牌索引
---@param card_type string @ 卡牌类型："minion" 或 "spell"
---@return boolean @ 是否购买成功
function ReqRecruitPhase:buyCard(card_index, card_type)
  local player = self.player
  local room = self.room
  
  if card_type == "minion" then
    return player:buyMinion(card_index)
  elseif card_type == "spell" then
    return player:buySpell(card_index)
  end
  
  return false
end

--- 刷新商店（保留锁定的卡牌）
function ReqRecruitPhase:refreshShop()
  local player = self.player
  local room = self.room
  
  -- 检查是否有足够金币
  if player.gold < 1 then
    room:sendLog {
      type = "#BattlegroundsNotEnoughGold",
      arg = player.id,
      arg2 = 1,
    }
    return false
  end
  
  -- 扣除金币
  player.gold = player.gold - 1
  
  -- 更新商店卡牌
  self:updateCards()
  
  room:sendLog {
    type = "#BattlegroundsShopRefreshed",
    arg = player.id,
    arg2 = player.gold,
  }
  
  return true
end

--- 升级酒馆
---@return boolean @ 是否升级成功
function ReqRecruitPhase:upgradeTavern()
  local player = self.player
  local room = self.room
  
  return player:upgradeTavern()
end

return ReqRecruitPhase